/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moose.irc.extension;

namespace moose.irc.actions.responses {


    [CLSCompliant(true)]
    [Action(
        Name = "moose-std-rsp-353",
        Description = "Handles Raw 353 responses, which is channel user lists",
        Author = "Mike MacKenzie-Jamer<email:mikejamer@gmail.com>",
        Version = "0.1",
        Type = ActionTypes.Response)]
    [Trigger(TriggerText = "353")]
    public sealed class Raw353Response : IrcAction {
        public override IrcSessionStates valid_states() {
            return IrcSessionStates.Registered;
        }

        /// <summary>
        /// For now, quietly update the users in the channel, will let 366 trigger the event
        /// </summary>
        public override void action(IrcSession session, IrcMessage message) { //there seems to be a message parsing problem here...
            IrcChannel channel = (IrcChannel)session.get_entity(message.Arguments[2]);
            char mode = (char)0x0;

            string user_string = null;
            foreach (string user in message.Arguments[message.Arguments.Length - 1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) {
                user_string = user.Trim();
                if (user_string[0] == '@' || user_string[0] == '+') {
                    mode = user_string[0];
                    user_string = user_string.Substring(1);
                }
                channel.add_user((IrcUser)session.get_entity(user_string));

                if (mode == '@')
                    channel.update_modes("+o " + user_string);
                else if (mode == '+')
                    channel.update_modes("+v " + user_string);
                mode = (char)0x0;
            }
            
            return;
        }
    }
}
